package com.itmuch.youran.security;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.itmuch.youran.domain.member.entity.Member;
import com.itmuch.youran.service.member.MemberService;

@Component
public class SubjectUtil {

	@Value("${youran.token.header}")
	private String tokenHeader;

	@Autowired
	private TokenUtils tokenUtils;

	@Autowired
	private MemberService memberService;

	public Member getSubject() {

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		String token = request.getHeader(this.tokenHeader);
		String username = tokenUtils.getUsernameFromToken(token);
		Member member = memberService.findbyUsername(username);
		return member;
	}

	public Integer getSubjectId(){
		return this.getSubject().getId();
	}

}
